<template>
  <view class="root-view">
    <view class="welcome">
      <view class="welcome-words">
        <view>
          <text>Hi.感谢你成为文朵健康会员</text>
        </view>
        <view>
          <text>正式开始之前，先让我们认识你吧</text>
        </view>
      </view>
      <button
        class="login"
        type="primary"
        openType="getPhoneNumber"
        @getphonenumber="onPhoneNumberGet"
      >
        授权登录
      </button>
    </view>
  </view>
</template>

<script lang="ts">
import "./index.less";
import Taro from "@tarojs/taro";
import { getCustomerInfo, getEncryptedData } from "../../services/user-service";

import {
  LSBluetoothPlugin,
  isBluetoothAvailable,
  addEventListener
} from "../../utils/deviceMananer.js";
export default {
  components: {},
  data() {
    return {
      isLogin: true
    };
  },
  methods: {
    async onPhoneNumberGet(e) {
      try {
        const res = await getEncryptedData(e.detail.encryptedData, e.detail.iv);
        this.getAndSaveUserInfo(res.data.data.phoneNumber);
      } catch (error) {
        console.log(error);
      }
    },
    async getAndSaveUserInfo(mobile) {
      const customerUserInfo = await getCustomerInfo();
      const customerId = customerUserInfo.id;
      if (customerId) {
        this.LSDeviceUserDataSync(customerId);
      }
      this.$store.commit("setUserInfo", {
        mobile,
        name: customerUserInfo.name
      });
      Taro.redirectTo({
        url: "/pages/index/index"
      });
    },
    LSDeviceUserDataSync(customerId) {
      addEventListener("bluetoothStateChange", res => {
        console.log(`蓝牙状态发生改变${JSON.stringify(res)}`);
      });
      //注册设备连接监听
      addEventListener("connectionStateChange", device => {
        console.log(`设备连接状态发现改变${JSON.stringify(device)}`);
      });
      //注册数据回调监听
      addEventListener("dataChanged", (deviceMac, dataType, data, dataStr) => {
        console.log(dataStr);
      });

      // initManager(() => {
      //   "蓝牙设备初始化";
      // });

      const LSDeviceLoginConfig = {
        appId: "wx43b1ab446b5db1e0",
        tenantName: "wenduo-embed", // 租户名称
        tenantId: 23, // 租户id
        subscriptionId: 2301, // 订阅id
        associatedId: customerId, // 第三方关联userId
        debug: true, // 打开H5的vconsole
        env: "online" // 接口环境：beta测试，online生产,
      };

      const that = this;
      LSBluetoothPlugin.login({
        ...LSDeviceLoginConfig,
        success(res) {
          console.info(res);
          console.info("用户信息已同步!");

          //判断手机蓝牙是否可用
          if (isBluetoothAvailable()) {
            console.log("蓝牙已开启！");
            //连接设备
            that.connectDevice();
          } else {
            //提示打开手机蓝牙
            console.log("蓝牙未开启！");
          }
        },
        fail(error) {
          console.error(error);
        }
      });
    },
    connectDevice() {}
  },

  async created() {},
  mounted() {},

  // onLoad
  onLoad() {},

  // onReady
  onReady() {},

  // 对应 onShow
  onShow() {},

  // 对应 onHide
  onHide() {},

  // 对应 onPullDownRefresh
  onPullDownRefresh() {}
};
</script>
